package com.imprenta.ejb;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.hibernate.util.HibernateUtil;
import com.imprenta.model.CatalogoBase;

public class EjbCatalogos {

	@SuppressWarnings("rawtypes")
	public static ArrayList<CatalogoBase> getListCatalogos(String _producto, String _catalogo){
		ArrayList<CatalogoBase> catalogoTamanio = new ArrayList<CatalogoBase>();
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String q = "select ccp.confcaractproductoid,p.descripcion,mc.descripcion, ic.descripcion,p.productoid" +
					" from Confcaractproducto ccp inner join ccp.itemcatalogo ic" +
					" inner join ccp.itemcatalogo.maestrocatalogo mc inner join" +
					" ccp.productoatributo pa inner join pa.producto as p where p.descripcion" +
					" like :producto and mc.descripcion like " + ":catalogo";
			Query query = session.createQuery(q);
			query.setParameter("producto", _producto);
			query.setParameter("catalogo", _catalogo);
			
			List l = query.list();

			for (int i = 0; i < l.size(); i++) {
				Object[] items = (Object[]) l.get(i);
				CatalogoBase catalogoBase = new CatalogoBase();
				
				catalogoBase.setId(Integer.parseInt(items[0].toString()));
				catalogoBase.setProducto(items[1].toString());
				catalogoBase.setCatalogo(items[2].toString());
				catalogoBase.setDescripcion(items[3].toString());
				catalogoBase.setProductoid(Integer.parseInt(items[4].toString()));
				
				catalogoTamanio.add(catalogoBase);
			}
			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}		
		finally{
			if (session != null) {
				session.close();
			}
		}
		return catalogoTamanio;
	}//end getlistcatalogos
	
	public static Object [] getPrecioByCatalogo(int _idTam, int _idPapel, int _idTintas, int _idPlastificado){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Object [] a = new Object[4];
		try {
			String q = "select ccp.precio from Confcaractproducto ccp where " +
					"ccp.confcaractproductoid in (:idTam, :idPapel, :idTintas, :idPlastificado)";
			Query query = session.createQuery(q);
			query.setParameter("idTam", _idTam);query.setParameter("idPapel", _idPapel);
			query.setParameter("idTintas", _idTintas);query.setParameter("idPlastificado", _idPlastificado);
			a = query.list().toArray();
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return a;
		
	}//end getpreciobyid
	
	public static String getProductoById(int _id){
		
		String descripcionProducto="";
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String q = "select ic.descripcion" +
					" from Confcaractproducto ccp inner join ccp.itemcatalogo ic" +
					" inner join ccp.itemcatalogo.maestrocatalogo mc inner join" +
					" ccp.productoatributo pa inner join pa.producto as p" +
					" where ccp.confcaractproductoid = :id";
			Query query = session.createQuery(q);
			query.setParameter("id", _id);
			descripcionProducto = query.list().get(0).toString();
			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return descripcionProducto;
		
	}//end getproductobyid
}
